﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DBService;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit.MEP.Opening;
using YArchitech.Revit.MEP.Opening.Common;

namespace HYRMEOpening
{
	public partial class DlgCreateOpening : System.Windows.Forms.Form
	{
		public static DlgCreateOpening GetInstance(UIApplication uiApp, bool service, string fileName)
		{
			if (DlgCreateOpening.dco == null || DlgCreateOpening.dco.IsDisposed)
			{
				DlgCreateOpening.dco = new DlgCreateOpening(uiApp, service, fileName);
			}
			return DlgCreateOpening.dco;
		}

		public DlgCreateOpening(UIApplication uiApp, bool server, string fileName)
		{
			this.IsServer = server;
			this.UiDoc = uiApp.ActiveUIDocument;
			this.UiApp = uiApp;
			this.mFileName = fileName;
			this.Request = new Request();
			List<DbOpeningBase> list = new List<DbOpeningBase>();
			if (server)
			{
				DbProject projectByCurrentDoc = DbProject.GetProjectByCurrentDoc(this.UiDoc.Document);
				if (projectByCurrentDoc != null)
				{
					foreach (DbWallToBeOpened dbWallToBeOpened in DbWallToBeOpened.GetByProject(projectByCurrentDoc))
					{
						if (dbWallToBeOpened.Status != OpeningStatus.Opened)
						{
							list.Add(dbWallToBeOpened);
						}
					}
					foreach (DbFloorToBeOpened dbFloorToBeOpened in DbFloorToBeOpened.GetByProject(projectByCurrentDoc))
					{
						if (dbFloorToBeOpened.Status != OpeningStatus.Opened)
						{
							list.Add(dbFloorToBeOpened);
						}
					}
					using (List<DbBeamToBeOpened>.Enumerator enumerator3 = DbBeamToBeOpened.GetByProject(projectByCurrentDoc).GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							DbBeamToBeOpened dbBeamToBeOpened = enumerator3.Current;
							if (dbBeamToBeOpened.Status != OpeningStatus.Opened)
							{
								list.Add(dbBeamToBeOpened);
							}
						}
						goto IL_251;
					}
				}
				MessageBox.Show("服务器连接失败或此模型未加入任何项目，请配置服务器、创建项目后操作。");
			}
			else
			{
				FileInfo fileInfo = new FileInfo(this.mFileName);
				if (fileInfo.Exists)
				{
					foreach (DbWallToBeOpened dbWallToBeOpened2 in DbWallToBeOpened.ReadFromXML(fileInfo.FullName))
					{
						if (dbWallToBeOpened2.Status != OpeningStatus.Opened)
						{
							list.Add(dbWallToBeOpened2);
						}
					}
					foreach (DbFloorToBeOpened dbFloorToBeOpened2 in DbFloorToBeOpened.ReadFromXML(fileInfo.FullName))
					{
						if (dbFloorToBeOpened2.Status != OpeningStatus.Opened)
						{
							list.Add(dbFloorToBeOpened2);
						}
					}
					using (List<DbBeamToBeOpened>.Enumerator enumerator3 = DbBeamToBeOpened.ReadFromXML(fileInfo.FullName).GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							DbBeamToBeOpened dbBeamToBeOpened2 = enumerator3.Current;
							if (dbBeamToBeOpened2.Status != OpeningStatus.Opened)
							{
								list.Add(dbBeamToBeOpened2);
							}
						}
						goto IL_251;
					}
				}
				MessageBox.Show("此模型开洞数据文件不存在，请先运行'提资'功能。");
				base.Close();
			}
			IL_251:
			Document document = this.UiDoc.Document;
			WorksetTable worksetTable = document.GetWorksetTable();
			WorksetId activeWorksetId = worksetTable.GetActiveWorksetId();
			Workset workset = worksetTable.GetWorkset(activeWorksetId);
			List<DbOpeningBase> list2 = new List<DbOpeningBase>();
			if (workset.IsEditable)
			{
				for (int i = 0; i < list.Count; i++)
				{
					DbOpeningBase dbOpeningBase = list[i];
					ElementId elementId = new ElementId(dbOpeningBase.HostId);
					if (document.GetElement(elementId) != null)
					{
						dbOpeningBase.IsPrimaryStructureOpening = CmdTools.IsPrimaryStructure(elementId, document);
						list2.Add(dbOpeningBase);
					}
				}
			}
			this.OpiningList = new BindingList<DbOpeningBase>(list2);
			this.InitializeComponent();
			if (server)
			{
				this.rbByFile.Checked = false;
				this.rbByServer.Checked = true;
				this.tbFileName.Visible = false;
				return;
			}
			this.rbByFile.Checked = true;
			this.rbByServer.Checked = false;
			this.tbFileName.Visible = true;
			this.tbFileName.Text = fileName;
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void DlgOpeningDetect_Load(object sender, EventArgs e)
		{
			Document document = this.UiDoc.Document;
			WorksetTable worksetTable = document.GetWorksetTable();
			WorksetId activeWorksetId = worksetTable.GetActiveWorksetId();
			Workset workset = worksetTable.GetWorkset(activeWorksetId);
			try
			{
				BindingList<DbOpeningBase> bindingList = new BindingList<DbOpeningBase>();
				if (workset == null)
				{
					IEnumerator<DbOpeningBase> enumerator = this.OpiningList.GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							DbOpeningBase dbOpeningBase = enumerator.Current;
							if (dbOpeningBase is DbWallToBeOpened)
							{
								ElementId elementId = new ElementId(((DbWallToBeOpened)dbOpeningBase).HostId);
								if (document.GetElement(elementId) != null)
								{
									bindingList.Add(dbOpeningBase);
									dbOpeningBase.AddOpeningTag(this.UiApp, TagStyle.PositionAndSize);
								}
								else
								{
									dbOpeningBase.IsCurrentProject = false;
									bindingList.Add(dbOpeningBase);
								}
							}
							else if (dbOpeningBase is DbFloorToBeOpened)
							{
								ElementId elementId2 = new ElementId(((DbFloorToBeOpened)dbOpeningBase).HostId);
								if (document.GetElement(elementId2) != null)
								{
									bindingList.Add(dbOpeningBase);
									dbOpeningBase.AddOpeningTag(this.UiApp, TagStyle.PositionAndSize);
								}
								else
								{
									dbOpeningBase.IsCurrentProject = false;
									bindingList.Add(dbOpeningBase);
								}
							}
							else if (dbOpeningBase is DbBeamToBeOpened)
							{
								ElementId elementId3 = new ElementId(((DbBeamToBeOpened)dbOpeningBase).HostId);
								if (document.GetElement(elementId3) != null)
								{
									bindingList.Add(dbOpeningBase);
									dbOpeningBase.AddOpeningTag(this.UiApp, TagStyle.PositionAndSize);
								}
								else
								{
									dbOpeningBase.IsCurrentProject = false;
									bindingList.Add(dbOpeningBase);
								}
							}
						}
						goto IL_2F1;
					}
				}
				if (!workset.IsEditable)
				{
					MessageBox.Show("当前工作集不可编辑！");
					base.Close();
					return;
				}
				foreach (DbOpeningBase dbOpeningBase2 in this.OpiningList)
				{
					if (dbOpeningBase2 is DbWallToBeOpened)
					{
						ElementId elementId4 = new ElementId(((DbWallToBeOpened)dbOpeningBase2).HostId);
						Element element = document.GetElement(elementId4);
						if (element != null)
						{
							if (!(element.WorksetId == activeWorksetId))
							{
								continue;
							}
							dbOpeningBase2.IsCurrentProject = true;
							bindingList.Add(dbOpeningBase2);
							try
							{
								dbOpeningBase2.AddOpeningTag(this.UiApp, TagStyle.PositionAndSize);
								continue;
							}
							catch
							{
								continue;
							}
						}
						dbOpeningBase2.IsCurrentProject = false;
						bindingList.Add(dbOpeningBase2);
					}
					else if (dbOpeningBase2 is DbFloorToBeOpened)
					{
						ElementId elementId5 = new ElementId(((DbFloorToBeOpened)dbOpeningBase2).HostId);
						Element element2 = document.GetElement(elementId5);
						if (element2 != null)
						{
							if (!(element2.WorksetId == activeWorksetId))
							{
								continue;
							}
							dbOpeningBase2.IsCurrentProject = true;
							bindingList.Add(dbOpeningBase2);
							try
							{
								dbOpeningBase2.AddOpeningTag(this.UiApp, TagStyle.PositionAndSize);
								continue;
							}
							catch
							{
								continue;
							}
						}
						dbOpeningBase2.IsCurrentProject = false;
						bindingList.Add(dbOpeningBase2);
					}
					else if (dbOpeningBase2 is DbBeamToBeOpened)
					{
						ElementId elementId6 = new ElementId(((DbBeamToBeOpened)dbOpeningBase2).HostId);
						Element element3 = document.GetElement(elementId6);
						if (element3 != null)
						{
							if (element3.WorksetId == activeWorksetId)
							{
								dbOpeningBase2.IsCurrentProject = true;
								bindingList.Add(dbOpeningBase2);
							}
						}
						else
						{
							dbOpeningBase2.IsCurrentProject = false;
							bindingList.Add(dbOpeningBase2);
						}
					}
				}
				IL_2F1:
				if (bindingList.Count <= 0)
				{
					MessageBox.Show("当前工作集无需开洞。");
					base.Close();
				}
				else
				{
					DbOpeningBase.SortReportsByDisplayOrder(ref bindingList);
					BindingList<DbOpeningBase> bindingList2 = new BindingList<DbOpeningBase>((from opening in bindingList
					where opening.IsPrimaryStructureOpening
					select opening).ToList<DbOpeningBase>());
					BindingList<DbOpeningBase> dataSource = new BindingList<DbOpeningBase>(bindingList.ToList<DbOpeningBase>().Except(bindingList2).ToList<DbOpeningBase>());
					this.dgvOpeningListArch.DataSource = dataSource;
					this.dgvOpeningList.DataSource = bindingList2;
					this.TurnToView3D(document);
					ConfigDataOperate.OpenHoleConfigData openHoleConfigData = new ConfigDataOperate.OpenHoleConfigData(ConfigDataOperate.InitOpenHoleDataConfigFile());
					this.checkBoxOpeningMin.Checked = true;
					this.textBoxOpeningMin.Text = openHoleConfigData.ArchOpeningMinSize.ToString("F0");
					this.MakeAllOpeningStatus(true);
					this.tabControl.SelectedIndex = 1;
					this.UpdateGridViewDataShow();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				base.Close();
			}
		}

		private void MakeAllOpeningStatus(bool bOpened = true)
		{
			foreach (DbOpeningBase dbOpeningBase in this.OpiningList)
			{
				dbOpeningBase.Status = (bOpened ? OpeningStatus.Opened : OpeningStatus.Unopen);
			}
			this.Refresh();
		}

		private void UpdateGridViewDataShow()
		{
			foreach (object obj in this.dgvOpeningList.Columns)
			{
				DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
				dataGridViewColumn.DefaultCellStyle.BackColor = System.Drawing.Color.DarkGray;
				dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
				if (dataGridViewColumn.Name.Equals("OpeningStatusString") || dataGridViewColumn.Name.Equals("IsCurrentProject"))
				{
					dataGridViewColumn.Visible = false;
				}
				if (dataGridViewColumn.Name.Equals("BoxParameterString"))
				{
					dataGridViewColumn.ReadOnly = true;
				}
			}
			foreach (object obj2 in ((IEnumerable)this.dgvOpeningList.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj2;
				int num = dataGridViewRow.Index + 1;
				dataGridViewRow.Cells["colNumber"].Value = num.ToString();
			}
			this.UpdateArchOpeningGridView();
		}

		private void TurnToView3D(Autodesk.Revit.DB.Document doc)
		{
			if (!(this.UiDoc.ActiveView is View3D))
			{
				View3D view3D = null;
				Transaction transaction = new Transaction(doc, "DelView");
				try
				{
					transaction.Start();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
					filteredElementCollector.OfClass(typeof(View3D));
					foreach (Element element in filteredElementCollector.ToElements())
					{
						if (element.Name == "盈建科开洞浏览")
						{
							view3D = (element as View3D);
							break;
						}
					}
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
				}
				if (view3D == null)
				{
					Transaction transaction2 = new Transaction(doc, "cv");
					try
					{
						transaction2.Start();
						FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(doc);
						filteredElementCollector2.WhereElementIsNotElementType();
						filteredElementCollector2.ToElements();
                        ViewFamilyType viewFamilyType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().FirstOrDefault((ViewFamilyType x) => 102 == (int)x.ViewFamily);
						view3D = View3D.CreateIsometric(doc, viewFamilyType.Id);
						view3D.Name = "盈建科开洞浏览";
						view3D.DetailLevel = (ViewDetailLevel)3;
                        view3D.DisplayStyle = (DisplayStyle)6;
						new List<ElementId>();
						transaction2.Commit();
					}
					catch
					{
						transaction2.RollBack();
						base.Close();
					}
				}
				this.UiDoc.ActiveView = view3D;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.ClearTag);
			base.Close();
		}

		private void dgvOpeningList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.CheckOpeningElement();
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (this.tabControl.SelectedIndex == 1)
			{
				if (this.dgvOpeningList.SelectedRows.Count > 0)
				{
					if (this.dgvOpeningList.SelectedRows.Count == 1)
					{
						int index = this.dgvOpeningList.SelectedRows[0].Index;
						new DlgRemark(((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[index]).ShowDialog();
					}
					else
					{
						List<DbOpeningBase> list = new List<DbOpeningBase>();
						foreach (object obj in this.dgvOpeningList.SelectedRows)
						{
							int index2 = ((DataGridViewRow)obj).Index;
							DbOpeningBase item = ((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[index2];
							list.Add(item);
						}
						new DlgRemark(list).ShowDialog();
					}
				}
				this.dgvOpeningList.Refresh();
				return;
			}
			if (this.dgvOpeningListArch.SelectedRows.Count > 0)
			{
				if (this.dgvOpeningListArch.SelectedRows.Count == 1)
				{
					int index3 = this.dgvOpeningListArch.SelectedRows[0].Index;
					new DlgRemark(((BindingList<DbOpeningBase>)this.dgvOpeningListArch.DataSource)[index3]).ShowDialog();
				}
				else
				{
					List<DbOpeningBase> list2 = new List<DbOpeningBase>();
					foreach (object obj2 in this.dgvOpeningListArch.SelectedRows)
					{
						int index4 = ((DataGridViewRow)obj2).Index;
						DbOpeningBase item2 = ((BindingList<DbOpeningBase>)this.dgvOpeningListArch.DataSource)[index4];
						list2.Add(item2);
					}
					new DlgRemark(list2).ShowDialog();
				}
			}
			this.dgvOpeningListArch.Refresh();
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			try
			{
				try
				{
                    //List<ViewPlan> list = (from Autodesk.Revit.DB.ViewPlan v in new FilteredElementCollector(this.UiDoc.Document).OfClass(typeof(ViewPlan))
                    //where !v.IsTemplate
                    //select v).ToList<ViewPlan>();
                    //IEnumerator<string> enumerator = (from v in this.OpiningLis)
                    //select v.LevelString).Distinct<string>().GetEnumerator();
                    //{
                    //    while (enumerator.MoveNext())
                    //    {
                    //        string strOpeningLevel = enumerator.Current;
                    //        string strName = strOpeningLevel + "(洞图)";
                    //        if (list.Find((ViewPlan p) => p.GenLevel.Name == strName) == null)
                    //        {
                    //            ViewPlan viewPlan = list.Find((ViewPlan v) => v.GenLevel.Name == strOpeningLevel);
                    //            if (viewPlan != null)
                    //            {
                    //                Transaction transaction = new Transaction(this.UiDoc.Document, "CopyViewPlan");
                    //                try
                    //                {
                    //                    transaction.Start();
                    //                    ElementId elementId = viewPlan.Duplicate(0);
                    //                    (this.UiDoc.Document.GetElement(elementId) as ViewPlan).ViewName = strOpeningLevel + "(洞图)";
                    //                    transaction.Commit();
                    //                }
                    //                catch
                    //                {
                    //                    transaction.RollBack();
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
				}
				catch
				{
				}
				Document document = this.UiDoc.Application.ActiveUIDocument.Document;
				Transaction transaction2 = new Transaction(document, "RemoveInvalidOpening");
				try
				{
					transaction2.Start();
					foreach (DbOpeningBase dbOpeningBase in this.m_lstInvalidOpenings)
					{
						document.Delete(dbOpeningBase.OpeningID);
					}
					transaction2.Commit();
				}
				catch
				{
					transaction2.RollBack();
				}
				ProgressBarForm progressBarForm = new ProgressBarForm(this.dgvOpeningList.Rows.Count + this.dgvOpeningListArch.Rows.Count, ProgressBarStyle.Blocks, true);
				progressBarForm.Text = "正在进行开洞...";
				progressBarForm.Show();
				List<Element> list2 = new List<Element>();
				foreach (object obj in ((IEnumerable)this.dgvOpeningList.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					DbOpeningBase dbOpeningBase2 = ((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[dataGridViewRow.Index];
					if (dbOpeningBase2.Status == OpeningStatus.Opened)
					{
						Element item = dbOpeningBase2.Create(this.UiDoc.Application);
						list2.Add(item);
						dbOpeningBase2.RemoveOpeningTag(this.UiDoc.Document);
					}
					if (this.IsServer)
					{
						dbOpeningBase2.UpdateToDb();
					}
					else
					{
						dbOpeningBase2.UpdateToXML(this.mFileName);
					}
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						base.Close();
						return;
					}
				}
				foreach (object obj2 in ((IEnumerable)this.dgvOpeningListArch.Rows))
				{
					DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
					DbOpeningBase dbOpeningBase3 = ((BindingList<DbOpeningBase>)this.dgvOpeningListArch.DataSource)[dataGridViewRow2.Index];
					if (dbOpeningBase3.Status == OpeningStatus.Opened)
					{
						Element item2 = dbOpeningBase3.Create(this.UiDoc.Application);
						list2.Add(item2);
						dbOpeningBase3.RemoveOpeningTag(this.UiDoc.Document);
					}
					if (this.IsServer)
					{
						dbOpeningBase3.UpdateToDb();
					}
					else
					{
						dbOpeningBase3.UpdateToXML(this.mFileName);
					}
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						base.Close();
						return;
					}
				}
				new ConfigDataOperate.OpenHoleConfigData(ConfigDataOperate.InitOpenHoleDataConfigFile())
				{
					ArchOpeningMinSize = Convert.ToDouble(this.textBoxOpeningMin.Text)
				}.WriteCfgData2Xml();
				MessageBox.Show("开洞完成。");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public Request Request
		{
			get
			{
				return this.m_request;
			}
			private set
			{
				this.m_request = value;
			}
		}

		private void MakeRequest(RequestId request)
		{
			this.Request.Make(request);
		}

		private void DlgCreateOpening_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.MakeRequest(RequestId.ClearTag);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgCreateOpening_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgCreateOpening.SendMessage(CmdCreateOpening._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void dgvOpeningList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void DlgCreateOpening_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgCreateOpening.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgCreateOpening.keybd_event((byte)bVk, 0, dwFlags2, 0);
			DlgCreateOpening.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgCreateOpening.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void dgvOpeningList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvOpeningList.CurrentCell.OwningColumn.Name == "colChoose")
			{
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged += this.ComboBox_SelectedIndexChanged;
			}
		}

		private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			int index = this.dgvOpeningList.CurrentRow.Index;
			if (index > 0)
			{
				DbOpeningBase dbOpeningBase = ((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[index];
				if (((System.Windows.Forms.ComboBox)sender).Text.Trim() == "拒绝")
				{
					dbOpeningBase.Status = OpeningStatus.Invalid;
				}
			}
		}

		private void dgvOpeningList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
		}

		private void CheckOpeningElement()
		{
			if (this.tabControl.SelectedIndex == 0)
			{
				if (this.dgvOpeningListArch.SelectedRows.Count > 0)
				{
					int index = this.dgvOpeningListArch.SelectedRows[0].Index;
					Element element = DbOpeningBase.FindTag(((BindingList<DbOpeningBase>)this.dgvOpeningListArch.DataSource)[index], this.UiDoc.Document);
					if (element != null)
					{
						List<Element> list = new List<Element>();
						list.Add(element);
						RevitVersionFuncs.SetSelection(this.UiDoc.Selection, list);
						UIView uiview = null;
						foreach (UIView uiview2 in this.UiDoc.GetOpenUIViews())
						{
							if (uiview2.ViewId.Equals(this.UiDoc.ActiveView.Id))
							{
								uiview = uiview2;
								break;
							}
						}
						XYZ xyz = element.get_BoundingBox(this.UiDoc.ActiveView).Min;
						XYZ xyz2 = element.get_BoundingBox(this.UiDoc.ActiveView).Max;
						XYZ xyz3 = xyz2 - xyz;
						xyz3 *= 10.0;
						xyz += xyz3;
						xyz2 -= xyz3;
						uiview.ZoomAndCenterRectangle(xyz, xyz2);
						return;
					}
				}
			}
			else if (this.dgvOpeningList.SelectedRows.Count > 0)
			{
				int index2 = this.dgvOpeningList.SelectedRows[0].Index;
				Element element2 = DbOpeningBase.FindTag(((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[index2], this.UiDoc.Document);
				if (element2 != null)
				{
					List<Element> list2 = new List<Element>();
					list2.Add(element2);
					RevitVersionFuncs.SetSelection(this.UiDoc.Selection, list2);
					UIView uiview3 = null;
					foreach (UIView uiview4 in this.UiDoc.GetOpenUIViews())
					{
						if (uiview4.ViewId.Equals(this.UiDoc.ActiveView.Id))
						{
							uiview3 = uiview4;
							break;
						}
					}
					XYZ xyz4 = element2.get_BoundingBox(this.UiDoc.ActiveView).Min;
					XYZ xyz5 = element2.get_BoundingBox(this.UiDoc.ActiveView).Max;
					XYZ xyz6 = xyz5 - xyz4;
					xyz6 *= 10.0;
					xyz4 += xyz6;
					xyz5 -= xyz6;
					uiview3.ZoomAndCenterRectangle(xyz4, xyz5);
				}
			}
		}

		private void dgvOpeningList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			this.CheckOpeningElement();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			new DlgCondition(this.dgvOpeningList).ShowDialog(this);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			DlgCombineConfig dlgCombineConfig = new DlgCombineConfig();
			if (dlgCombineConfig.ShowDialog() == DialogResult.OK)
			{
				this.OpiningList = new BindingList<DbOpeningBase>(DbOpeningBase.Combine(this.OpiningList.ToList<DbOpeningBase>(), this.UiApp, dlgCombineConfig.Size, dlgCombineConfig.Length, dlgCombineConfig.IsBox));
				this.dgvOpeningList.DataSource = this.OpiningList;
				this.MakeRequest(RequestId.AddTag);
				foreach (object obj in ((IEnumerable)this.dgvOpeningList.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					dataGridViewRow.Cells["HostTypeString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["CutTypeString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["LevelString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["SectionParameterString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["OpeningStatusString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["HostTypeString"].Style.BackColor = System.Drawing.Color.Gray;
					dataGridViewRow.Cells["colNumber"].Style.BackColor = System.Drawing.Color.Gray;
				}
				foreach (object obj2 in ((IEnumerable)this.dgvOpeningList.Rows))
				{
					DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
					int num = dataGridViewRow2.Index + 1;
					dataGridViewRow2.Cells["colNumber"].Value = num.ToString();
				}
			}
		}

		private void btnSubmit_Click_1(object sender, EventArgs e)
		{
			try
			{
				foreach (object obj in ((IEnumerable)this.dgvOpeningList.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					DbOpeningBase dbOpeningBase = ((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[dataGridViewRow.Index];
					if (this.IsServer)
					{
						dbOpeningBase.UpdateToDb();
					}
					else
					{
						dbOpeningBase.UpdateToXML(this.mFileName);
					}
				}
				if (this.IsServer)
				{
					DbProject projectByCurrentDoc = DbProject.GetProjectByCurrentDoc(this.UiDoc.Document);
					if (projectByCurrentDoc != null)
					{
						foreach (DbUser dbUser in projectByCurrentDoc.GetAllMEPEngineer())
						{
							DbUser.System.SendMessage(dbUser.Name, "项目" + projectByCurrentDoc.Name + "开动数据已更新。");
						}
					}
				}
				MessageBox.Show("数据已保存。");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btnOpenHoleConfig_Click(object sender, EventArgs e)
		{
			new OpenHoleConfigForm().ShowDialog();
		}

		private void btnCheck_Click(object sender, EventArgs e)
		{
			this.CheckOpeningElement();
		}

		private void checkBoxOpeningMin_CheckedChanged(object sender, EventArgs e)
		{
			this.textBoxOpeningMin.Enabled = this.checkBoxOpeningMin.Checked;
			this.UpdateArchOpeningGridView();
		}

		private void textBoxOpeningMin_TextChanged(object sender, EventArgs e)
		{
			this.UpdateArchOpeningGridView();
		}

		private void UpdateArchOpeningGridView()
		{
			foreach (object obj in this.dgvOpeningListArch.Columns)
			{
				DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
				dataGridViewColumn.DefaultCellStyle.BackColor = System.Drawing.Color.DarkGray;
				dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
				if (dataGridViewColumn.Name.Equals("OpeningStatusString") || dataGridViewColumn.Name.Equals("IsCurrentProject"))
				{
					dataGridViewColumn.Visible = false;
				}
				if (dataGridViewColumn.Name.Equals("BoxParameterString"))
				{
					dataGridViewColumn.ReadOnly = true;
				}
			}
			double num = 0.0;
			if (!string.IsNullOrEmpty(this.textBoxOpeningMin.Text))
			{
				num = Convert.ToDouble(this.textBoxOpeningMin.Text);
			}
			CurrencyManager currencyManager = (CurrencyManager)this.BindingContext[this.dgvOpeningListArch.DataSource];
			currencyManager.SuspendBinding();
			int num2 = 0;
			List<DbOpeningBase> list = currencyManager.List.Cast<DbOpeningBase>().ToList<DbOpeningBase>();
			foreach (object obj2 in ((IEnumerable)this.dgvOpeningListArch.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj2;
				DbOpeningBase dbOpeningBase = list[num2++];
				bool flag;
				if (dbOpeningBase.SectionParameter.SectionType == DBService.SectionType.Arc)
				{
					flag = (dbOpeningBase.SectionParameter.Parameter1 + dbOpeningBase.BoxParameter.Parameter1 * 2.0 >= num);
				}
				else
				{
					flag = (dbOpeningBase.SectionParameter.Parameter1 + dbOpeningBase.BoxParameter.Parameter1 * 2.0 >= num && dbOpeningBase.SectionParameter.Parameter2 + dbOpeningBase.BoxParameter.Parameter1 * 2.0 >= num);
				}
				dataGridViewRow.Visible = (!this.checkBoxOpeningMin.Checked || flag);
				dbOpeningBase.Status = (dataGridViewRow.Visible ? OpeningStatus.Opened : OpeningStatus.Unopen);
			}
			currencyManager.ResumeBinding();
			foreach (object obj3 in ((IEnumerable)this.dgvOpeningListArch.Rows))
			{
				DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj3;
				int num3 = dataGridViewRow2.Index + 1;
				dataGridViewRow2.Cells["colNumberEx"].Value = num3.ToString();
			}
		}

		private void DlgCreateOpening_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					base.Close();
				}
			}
			catch
			{
			}
		}

		private void dgvOpeningListArch_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.CheckOpeningElement();
		}

		private void dgvOpeningList_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.CheckOpeningElement();
		}

		public ExternalCommandData cmdData;

		public UIDocument UiDoc;

		public UIApplication UiApp;

		public bool IsServer;

		private string mFileName;

		public BindingList<DbOpeningBase> OpiningList = new BindingList<DbOpeningBase>();

		public List<DbOpeningBase> m_lstInvalidOpenings = new List<DbOpeningBase>();

		private static DlgCreateOpening dco;

		private Request m_request;
	}
}
